/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.es.bxip.service;

import com.es.bxip.domain.Deal;
import com.es.bxip.domain.Deal;
import java.util.HashSet;

import java.util.Set;
import java.util.Set;

/**
 *
 * @author mruiz
 */
public class DealService {

    public static Set<Deal> getMostPopular() {
//        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
//        ProjectDAOJpaController pc = new ProjectDAOJpaController(null, factory);
//        return pc.findProjectDAOEntities(3, 0);

        Set<Deal> l = initializeDeals();
        return l;
    }

    public static Deal getDealById(Integer id) {
//        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
//        ProjectDAOJpaController pc = new ProjectDAOJpaController(null, factory);
//        return pc.findProjectDAO(id);

        Set<String> tags1 = new HashSet<String>();
        tags1.add("tapas");
        tags1.add("almuerzo");


        String des = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
        Deal d1 = new Deal(1, des, "Oferta 1", "Alberto Calleja", "01/01/2012", "02/03/2013", "C/Lalala 3", null);
        d1.setPrice(20.1);
        d1.setTags(tags1);


        return d1;
    }

    public static Set<Deal> getDealByCategory(Integer catId) {
        Set<String> tags = new HashSet<String>();
        Set<Deal> res = new HashSet<Deal>();
        if (catId == 1) {
            //Categoria comer
            tags.add("almuerzo");
            tags.add("tapas");
            tags.add("cena");
            res.addAll(getDealsByTags(tags));
        }
        return res;
    }

    public static Set<Deal> getDealsByTags(Set<String> tags) {

        Set<Deal> result = new HashSet<Deal>();

        //Creating all the deals :)
        Set<Deal> l = initializeDeals();
        for (String t : tags) {
            //TODO Search for these tags in the DB
            System.out.println("Searching by tag: " + t);
            for (Deal d : l) {
                if (d.getTags() != null && d.getTags().contains(t)) {
                    result.add(d);
                }
            }
        }
        System.out.println("Devolviendo" + result);
        return result;
    }

    private static Set<Deal> initializeDeals() {

        //Creating all the deals :)
        Set<Deal> l = new HashSet<Deal>();
        String des = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
        Deal d1 = new Deal(1, des, "Oferta 1", "Alberto Calleja", "01/01/2012", "02/03/2013", "C/Lalala 3", null);
        Deal d2 = new Deal(2, des, "Oferta 2", "Miguel Ruiz", "01/01/2012", "02/03/2013", "C/Lalala 3", null);
        Set<String> tags1 = new HashSet<String>();
        tags1.add("tapas");
        tags1.add("almuerzo");
        d1.setTags(tags1);
        l.add(d1);
        Set<String> tags2 = new HashSet<String>();
        tags2.add("tapas");
        tags2.add("cena");
        d2.setTags(tags2);
        l.add(d2);
        l.add(new Deal(3, des, "Oferta 3", "Miguel ruiz", "01/11/2013", "02/23/2013", "C/Lalala 3", null));
        l.add(new Deal(4, des, "Oferta 4", "Alberto Calleja", "01/04/2013", "02/23/2013", "C/Lalala 5", null));
        l.add(new Deal(5, des, "Oferta 4", "Alberto Calleja", "01/04/2013", "02/23/2013", "C/Lalala 2", null));
        l.add(new Deal(6, des, "Oferta 5", "Miguel Ruiz", "01/05/2013", "02/03/2013", "C/Lalala", null));
        return l;
    }

    public static Set<String> getAllTags() {
        Set<String> l = new HashSet<String>();
        l.add("cena");
        l.add("tapas");
        l.add("almuerzo");
        return l;
    }
}
